<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="关键字" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入场馆名字" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="开馆状态" clearable style="width: 240px">
          <el-option v-for="dict in statusList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['showroom:venue:add']">新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button v-if="queryParams.status == 0" type="success" plain size="mini" @click="changeVenue" v-hasPermi="['showroom:venue:open']">一键开馆</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-if="queryParams.status == 1" type="danger" plain size="mini" @click="changeVenue" v-hasPermi="['showroom:venue:close']">一键闭馆</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :height="tableHeight" v-loading="loading" :data="venueList" @selection-change="handleSelectionChange" @row-dblclick="handleUpdate" stripe border>
      <el-table-column v-if="this.queryParams.status" type="selection" width="55" />
      <el-table-column label="序号" type="index" align="center" width="60" />
      <el-table-column label="场馆名称" prop="name" align="center" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="场馆类型" prop="venueType" width="150" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.venueType == 0">竞购场馆</span>
          <span v-else-if="scope.row.venueType == 1">云派商城</span>
          <span v-else-if="scope.row.venueType == 3">置换商城</span>
          <span v-else>兑换商城</span>
        </template>
      </el-table-column>
      <el-table-column label="竞购时间" prop="startTime" width="150" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.startTime }} - {{ scope.row.endTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="场馆展示时间" prop="venueTimeStr" width="150" align="center" />
      <el-table-column label="围观人数" prop="watchPerson" width="120" align="center" />
      <el-table-column label="开馆状态" prop="title" width="120" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.showroom_venue_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="机器人回收速度(s)" prop="robotSpeed" width="150" align="center" v-if="checkPermi(['showroom:venue:edit'])" />
      <el-table-column label="竞价品止售比例" prop="stopGoodsRate" width="120" align="center" v-if="checkPermi(['showroom:venue:edit'])" />

      <el-table-column label="顺序" prop="orderNum" align="center" width="100" sortable />
      <el-table-column label="小图" width="200">
        <template slot-scope="scope">
          <table-image :imgDicUrls="scope.row.imgMin"></table-image>
        </template>
      </el-table-column>
      <el-table-column label="中图" width="200">
        <template slot-scope="scope">
          <table-image :imgDicUrls="scope.row.imgMiddle"></table-image>
        </template>
      </el-table-column>

      <el-table-column label="大图" width="200">
        <template slot-scope="scope">
          <table-image :imgDicUrls="scope.row.imgMax"></table-image>
        </template>
      </el-table-column>

      <el-table-column label="场馆简介" width="200">
        <template slot-scope="scope">
          <table-image :imgDicUrls="scope.row.introduction"></table-image>
        </template>
      </el-table-column>

      <el-table-column label="检测报告" width="200">
        <template slot-scope="scope">
           <table-image :imgDicUrls="scope.row.checkReport"></table-image>
        </template>
      </el-table-column>
      <el-table-column label="物流信息" width="200">
        <template slot-scope="scope">
          <table-image :imgDicUrls="scope.row.deliverReport"></table-image>
        </template>
      </el-table-column>
      <el-table-column label="开馆之礼信息" align="center">
        <el-table-column label="开馆前开始时间(分钟)" prop="giftBeginTime" width="180" align="center" />
        <el-table-column label="开馆前结束时间(分钟)" prop="giftEndTime" width="180" align="center" />
        <el-table-column label="启用状态" prop="useState" width="100" align="center">
          <template slot-scope="scope">
            {{selectDictLabel(userState,scope.row.useState)}}
          </template>
        </el-table-column>
        <el-table-column label="开馆之礼介绍" prop="venueRemark" width="150" align="center" />
        <el-table-column label="展示图" prop="infoImgShow" width="150">
          <template slot-scope="scope">
            <table-image :imgDicUrls="scope.row.infoImgShow"></table-image>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['showroom:venue:edit']">修改</el-button>
          <el-button size="mini" type="text" @click.stop="clearVenue(scope.row)" v-hasRole="['admin']">清缓存</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改角色配置对话框 -->
    <Z-Dialog :title="title" :visible.sync="open" @confirm="submitForm">
      <el-form ref="form" :model="form" label-width="150px">

        <el-form-item label="场馆名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入标题" :disabled="form.name?true:false" />
        </el-form-item>
        <el-form-item label="场馆类型" prop="venueType">
          <el-select v-model="form.venueType" placeholder="场馆类型选择" clearable style="width: 100%" disabled>
            <el-option v-for="dict in dict.type.showroom_venue_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-time-select style="width: 100%" v-model="form.startTime" :picker-options="{
              start: '08:00',
              step: '00:01',
              end: '23:00',
            }" placeholder="选择开始时间">
          </el-time-select>
        </el-form-item>
        <el-form-item label="竞购结束时间" prop="endTime">
          <el-time-select style="width: 100%" v-model="form.endTime" :picker-options="{
              start: form.startTime?form.startTime:'08:00',
              step: '00:01',
              end: '23:00',
            }" placeholder="选择结束时间">
          </el-time-select>
        </el-form-item>
        <el-form-item label="展示结束时间" prop="showEndTime">
          <el-time-select style="width: 100%" v-model="form.showEndTime" :picker-options="{
              start: form.startTime?form.startTime:'08:00',
              step: '00:01',
              end: '23:00',
            }" placeholder="选择展示结束时间">
          </el-time-select>
        </el-form-item>
        <el-form-item label="围观人数" prop="watchPerson">
          <el-input v-model="form.watchPerson" placeholder="请输入围观人数" />
        </el-form-item>
        <el-form-item label="开馆状态" prop="status">
          <el-select v-model="form.status" placeholder="开馆状态选择" clearable style="width: 100%">
            <el-option v-for="dict in dict.type.showroom_venue_status" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="机器人回收速度(s)" prop="robotSpeed">
          <el-input-number v-model="form.robotSpeed" controls-position="right" :min="1" :max="10" />最大值为10
        </el-form-item>
        <el-form-item label="竞价品止售比例" prop="stopGoodsRate">
          <el-input-number v-model="form.stopGoodsRate" controls-position="right" :min="0" :max="1" />最大值为1
        </el-form-item>
        <div v-if="form.venueType==0">
          <el-form-item label="开馆前开始时间" prop="giftBeginTime">
            <el-input v-model="form.giftBeginTime" placeholder="时间(分钟)" />
          </el-form-item>
          <el-form-item label="开馆前结束时间" prop="giftEndTime">
            <el-input v-model="form.giftEndTime" placeholder="时间(分钟)" />
          </el-form-item>
          <el-form-item label="启用状态" prop="useState">
            <el-select v-model="form.useState" placeholder="启用状态" clearable style="width: 100%">
              <el-option v-for="dict in userState" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="开馆之礼介绍" prop="venueRemark">
            <el-input v-model="form.venueRemark" placeholder="请输入介绍" />
          </el-form-item>
          <el-form-item label="开馆之礼展示图" prop="infoImgShow">
            <image-upload :valueData="form.infoImgShow" params="infoImgShow" @uploaded="uploaded"></image-upload>
          </el-form-item>
        </div>

        <el-form-item label="场馆小图" prop="imgMin">
          <image-upload :valueData="form.imgMin" params="imgMin" @uploaded="uploaded"></image-upload>
        </el-form-item>
        <el-form-item label="场馆中图" prop="imgMiddle">
          <image-upload :valueData="form.imgMiddle" params="imgMiddle" @uploaded="uploaded"></image-upload>
        </el-form-item>
        <el-form-item label="场馆大图" prop="imgMax">
          <image-upload :valueData="form.imgMax" params="imgMax" @uploaded="uploaded"></image-upload>
        </el-form-item>

        <el-form-item label="场馆简介" prop="introduction">
          <image-upload :valueData="form.introduction" params="introduction" @uploaded="uploaded"></image-upload>
        </el-form-item>

        <el-form-item label="检测报告" prop="imgMax">
          <image-upload :valueData="form.checkReport" params="checkReport" @uploaded="uploaded"></image-upload>
        </el-form-item>

        <el-form-item label="发货须知" prop="imgMax">
          <image-upload :valueData="form.deliverReport" params="deliverReport" @uploaded="uploaded"></image-upload>
        </el-form-item>
      </el-form>
    </Z-Dialog>
  </div>
</template>

<script>
import { listVenue, changeVenue, editVenue, refreshVenueCache } from "@/api/showroom/venue";
import tableImage from "@/components/TableImageView/index.vue";
import ImageUpload from "@/components/ImageUpload/index.vue";
import { deepClone } from '@/utils/index'
import { checkPermi } from "@/utils/permission"; // 权限判断函数
import hasPer from "@/directive/permission/hasPer.js";

export default {
  name: "VenueInformation", //场馆信息
  dicts: ["showroom_venue_status", "showroom_venue_type"],
  components: { tableImage, ImageUpload },
  data() {
    return {
      //选中的场馆
      seletVenueArray: [],
      //上传的图片
      dialogImageUrl: "",

      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 场馆信息列表数据
      venueList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
        name: undefined,
        isAsc: undefined,
        status: undefined, //0 停用 1 正常
      },

      // 表单参数
      form: {},

      userState: [{ label: '启用', value: '1' }, { label: '不启用', value: '0' }],

      changeVenueParams: {
        status: 0,
        ids: [],
      },
    };
  },
  computed: {
    tableHeight() {
      return 'calc(100vh - 280px)'
    },
    //状态列表添加全部
    statusList() {
      let statusDictArray = JSON.parse(
        JSON.stringify(this.dict.type.showroom_venue_status)
      );
      statusDictArray.unshift({ label: "全部", value: undefined });
      return statusDictArray;
    },
  },
  watch: {
    "queryParams.status": {
      deep: true,
      handler: function (newV, oldV) {
        this.changeVenueParams.status = newV == 1 ? 0 : 1; //开/闭关状态取反
        this.getList();
      },
    },
  },
  created() {
    this.getList();
  },
  methods: {
    checkPermi,
    /** 获取场馆信息列表 */
    getList() {
      this.loading = true;
      listVenue(this.queryParams)
        .then((response) => {
          if (response.code == 200) {
            this.venueList = response.data;
            this.venueList.forEach(item => {
              if (item.venueGiftInfo) {
                item.giftBeginTime = item.venueGiftInfo.beginTime;
                item.giftEndTime = item.venueGiftInfo.endTime;
                item.useState = String(item.venueGiftInfo.useState);
                item.venueRemark = item.venueGiftInfo.venusRemark;
                item.infoImgShow = item.venueGiftInfo.imgShow;

              }
              if (item.venueTimeStr) {
                let arr = item.venueTimeStr.split("-");
                item.showEndTime = arr[1];
              }

            });
            this.total = response.total;
          }
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    //列表选中
    handleSelectionChange(val) {
      this.seletVenueArray = val;
      this.changeVenueParams.ids = val.map((item) => {
        return item.id;
      });
    },
    //一键开/闭馆
    changeVenue() {
      if (!this.changeVenueParams.ids.length) {
        this.$modal.msgError("请选择场馆");
        return;
      }
      changeVenue(this.changeVenueParams)
        .then((response) => {
          if (response.code == 200) {
            this.$modal.msgSuccess("操作成功");
            this.changeVenueParams.ids = [];
            this.getList();
          }
        })
        .catch((err) => { });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        status: undefined,
        orderNum: undefined,
        imgMin: undefined,
        venueType: undefined,
        startTime: undefined,
        endTime: undefined,
        imgMax: undefined,
      };
    },

    // 关闭弹框，清空表单
    handleCloseDialogForm() { },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "场馆信息";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      if (!hasPer.isPerShow(['showroom:venue:edit'])) {
        return;
      }

      this.title = "编辑场馆";
      this.reset();
      this.open = true;
      this.form = deepClone(row);
      this.form.venueType = String(this.form.venueType);
      this.form.status = String(this.form.status);
    },

    clearVenue(row) {
      refreshVenueCache({ venueId: row.id })
        .then((response) => {
          this.$modal.msgSuccess("操作成功");
        })
    },

    /** 提交按钮 */
    submitForm: function () {
      this.form.venueTimeStr = this.form.startTime + '-' + this.form.showEndTime;
      editVenue(this.form)
        .then((response) => {
          this.$modal.msgSuccess("操作成功");
          this.changeVenueParams.ids = [];
          this.getList();
          this.open = false;
        })
        .catch((err) => { });
    },

    /**上传数据 */
    uploaded(params, dict) {
      this.form[params] = JSON.stringify(dict);
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除角色编号为"' + roleIds + '"的数据项？')
        .then(function () {
          // return delRole(roleIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
  },
};
</script>
<style scoped>
</style>
